//목록리스트 1단계 액티비티
package kr.jin.zeroprojectone;

import java.util.ArrayList;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

public class DBListAcitiviryStep1 extends Activity {
	ArrayList <MyItemsDBList1> arList1 = new ArrayList<MyItemsDBList1>();
	DBListAdapterStep1 dblist1;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dblist_activity_step1);
		setAdapterAndList();
	}
	
	void setAdapterAndList(){
		//db목록 리스트 -STEP1
		DBHelper dbHelper= new DBHelper(this);
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		Cursor c1 = db.rawQuery("SELECT * FROM step1", null);	
		c1.moveToFirst();	
		c1.getCount();

		MyItemsDBList1 dbItem1;
				
		for(int i = 0; i<c1.getCount(); i++){		
			int id = c1.getInt(0);
			String date = c1.getString(1);
			String activity = c1.getString(2);
			Long time = c1.getLong(3);
			String text1 = getResources().getString(R.string.class_dblistactivity_timetext1);
			String text2 = getResources().getString(R.string.class_dblistactivity_timetext2);
			String text3 = getResources().getString(R.string.class_dblistactivity_timetext3);
			dbItem1 = new MyItemsDBList1(id,date,activity , String.format("%02d"+text1+"%02d"+text2+"%02d"+text3, time/1000/60/60, (time/1000/60)%60, (time/1000)%60));	
			arList1.add(dbItem1);
			c1.moveToNext();
		}
		dblist1 = new DBListAdapterStep1(this, R.layout.dbcustomlistview1, arList1);
		ListView DBListStep1 = (ListView)findViewById(R.id.dblist_activity_step1_listview);
		DBListStep1.setAdapter(dblist1);
		DBListStep1.setOnItemClickListener(mItemListener);
		
		dbHelper.close();
	}
	
	AdapterView.OnItemClickListener mItemListener = new AdapterView.OnItemClickListener() {
	
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			
			showDeleteDalog(1,position);
		}
	};
	
	void showDeleteDalog(int num, int position){
		FragmentManager fm1 = getFragmentManager();
		FragmentTransaction tr1 = fm1.beginTransaction();
		Fragment fg1 = fm1.findFragmentByTag("delete1");
		if(fg1!=null){
			tr1.remove(fg1);
		}
		FragmentDialogDeleteList fd1 = FragmentDialogDeleteList.newInstance(num, position);
		fd1.show(tr1, "delete1");
	}
	
	void DeleteListPositiveDialog(int num, int position){
		DBHelper dbHelper= new DBHelper(DBListAcitiviryStep1.this);
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		
		int i = arList1.get(position).id1;
		//int i = (Integer) parent.getAdapter().getItem(position);
		
		db.execSQL("DELETE FROM step1 WHERE _id = "+ i + ";");
		arList1.clear();
		dbHelper.close();
		setAdapterAndList();
	};
}
